import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.Types;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Date;

/**
 * @owner: bwx1115623
 * @time:  2023/1/30
 **/

public class Opengauss_Function_MultiNodeWrite_Case0044 {
	static public String driver = "org.postgresql.Driver";
	static public String tb_name;
    public static Properties getConfigFromFile(String filePath) {
        Properties props = new Properties();
        try {
            BufferedInputStream config = new BufferedInputStream(new FileInputStream(filePath));
            props.load(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return props;
    }

    public static String genURLFromPro(Properties props) {
        String hostname = props.getProperty("hostname");
        String[] hostnames = hostname.split(",");
        String port = props.getProperty("port");
        String[] ports = port.split(",");
        String dbname = props.getProperty("dbname");
		tb_name = props.getProperty("tbname");
        return genURLFromHostsPortsDBname(hostnames, ports, dbname);
    }

    public static String genURLFromHostsPortsDBname(String[] hostnames, String[] ports, String dbname) {
        String sourceURL;
        if (hostnames.length == 1) {
            sourceURL = "jdbc:postgresql://" + hostnames[0] + ":" + ports[0] + "/" + dbname;
        } else {
            ArrayList<String> ip_port_pare = new ArrayList<>();
            if (ports.length == 1) {
                for (String ip : hostnames) {
                    ip_port_pare.add(ip + ":" + ports[0]);
                }
            } else {
                for (int i = 0; i < hostnames.length; i++) {
                    ip_port_pare.add(hostnames[i] + ":" + ports[i]);
                }
            }
            sourceURL = "jdbc:postgresql://" + String.join(",", ip_port_pare) + "/" + dbname;
        }
        return sourceURL;
    }

    public static Connection GetConnection(Properties props) {
        Connection connR;
        String sourceURL = genURLFromPro(props);
        System.out.println(sourceURL);
        try {
            Class.forName(driver).newInstance();
            connR = DriverManager.getConnection(sourceURL, props);
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
        return connR;
    }
	  
	public static void CreateTable(Connection conn) throws InterruptedException {
		Statement stmt = null;
		try {
		    stmt = conn.createStatement();
		    int rc = stmt.executeUpdate("drop table if exists " + tb_name + ";create table " + tb_name + "(id int, name varchar(20));");
		    Thread.sleep(10000);
		    stmt.close();
		} catch (SQLException e) {
		    if (stmt != null) {
		        try {
		            stmt.close();
		        } catch (SQLException e1) {
		            e1.printStackTrace();
		        }
		    }
		    e.printStackTrace();
		}
	}

	 public static void BatchInsertData(Connection conn) throws InterruptedException {
	     PreparedStatement pst = null;
		 try {
		     pst = conn.prepareStatement("insert into " + tb_name + " values(?, ?);");
		     for (int i = 0; i < 100; i++) {
		         pst.setInt(1, i);
		         pst.setString(2, "data" + i);
		         pst.addBatch();
		     }
		         pst.executeBatch();
		         Thread.sleep(10000);
		         pst.close();
		 } catch (SQLException e) {
		     if (pst != null) {
		         try {
		             pst.close();
		         } catch (SQLException e1) {
		             e1.printStackTrace();
		         }
		     }
		     e.printStackTrace();
		 }
	 }

	public static void QuerySQL(Connection conn) {
	    Statement stmt = null;
		try {
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from " + tb_name + ";");
			while (rs.next()){
					System.out.println(rs.getInt("id") + " | " + rs.getString("name"));
				}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public static void DropStatement(Connection conn) {
		PreparedStatement pstmt = null;
		try {
		 	pstmt = conn.prepareStatement("drop table "+tb_name+";");
		    int rowcount2 = pstmt.executeUpdate();
		    pstmt.close();
		} catch (SQLException e) {
		    if (pstmt != null) {
		        try {
		            pstmt.close();
		        } catch (SQLException e1) {
		        e1.printStackTrace();
		        }
		    }
		    e.printStackTrace();
		}
    }

    public static void main(String[] args) throws InterruptedException {
        Properties pros = new Properties();
        Properties pros_conf=null;
        String jdbc_config_file = null;
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "--config-file":
                    jdbc_config_file = args[i + 1];
                    pros_conf=getConfigFromFile(jdbc_config_file);
                    break;
                case "-F":
                    jdbc_config_file = args[i + 1];
                    pros_conf=getConfigFromFile(jdbc_config_file);
                    break;
                default:
                    break;
            }
        }
        //建立连接
        if (pros_conf!=null){
            //以参数为准，覆盖配置文件中的
            pros_conf.putAll(pros);
        }
        Connection conn = GetConnection(pros_conf);
	    Statement stmt = null;
	    CreateTable(conn);
	    BatchInsertData(conn);
	    QuerySQL(conn);
		DropStatement(conn);

	    try {
	      conn.close();
	    } catch (SQLException e) {
	      e.printStackTrace();
	    }
	}
}
